/*
 * Copyright (c) 2001 Boris Pruessmann and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 *    David Corbin - Initial Implementation
 */
package net.sourceforge.p4eclipse.ui.wizards;

import java.util.Iterator;
import java.util.List;

import net.sourceforge.p4eclipse.core.api.PerforceUtil;
import net.sourceforge.p4eclipse.core.resources.RemoteFolder;
import net.sourceforge.p4eclipse.core.resources.RemoteResource;
import net.sourceforge.p4eclipse.ui.PerforceUIPlugin;
import net.sourceforge.p4eclipse.ui.model.AllRootsElement;
import net.sourceforge.p4eclipse.ui.model.RemoteContentProvider;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.ui.model.WorkbenchLabelProvider;

/**
  * @version 	1.0
  * @author		<a href="mailto:dcorbin@users.sourceforge.net">David Corbin</a>
  */
public class SelectDepotPathPage extends PerforceWizardPage
{
	public SelectDepotPathPage(String pageName)
	{
		super(pageName);
		setTitle("Connect Project to Perforce");
		setDescription("Map project location to a Perforce repository");
		setImageDescriptor(PerforceUIPlugin.getPlugin().getImageDescriptor(PerforceUIPlugin.IMG_REPOSITORY));
	}

	protected Text createTextField(Composite parent) 
	{
		Text text = new Text(parent, SWT.SINGLE | SWT.BORDER);
		
		GridData data = new GridData(GridData.FILL_BOTH);
		data.grabExcessVerticalSpace = false;
		data.grabExcessHorizontalSpace = true;
		data.widthHint = IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH;

		text.setLayoutData(data);
		return text;
	}

	protected TreeViewer createDepotTree(Composite parent)
	{
		AllRootsElement root = new AllRootsElement();
		
		TreeViewer viewer = new TreeViewer(parent, SWT.BORDER| SWT.SINGLE | SWT.H_SCROLL | SWT.V_SCROLL);
		viewer.setContentProvider(new RemoteContentProvider());	
		viewer.setLabelProvider(new WorkbenchLabelProvider());
		
		GridData data = new GridData(GridData.FILL_BOTH);
		data.grabExcessVerticalSpace = true;
		data.grabExcessHorizontalSpace = true;
		viewer.getControl().setLayoutData(data);
		viewer.setInput(root);
		return viewer;
	}
	TreeViewer tree;
	private void showDepotPath(String path)
	{
		try
		{
			final List elements = RemoteResource.splitRepositoryPath(path);
			String [] depots = PerforceUtil.depots();	
			for (int depotIndex=0; depotIndex<depots.length; depotIndex++)
			{
				if (depots[depotIndex].equals("//"+elements.get(0)))
				{
					setErrorMessage(null);
					elements.set(0,"//"+elements.get(0));
					Tree t = (Tree)tree.getControl();
					TreeItem currentNode = null;
					TreeItem[] items = t.getItems();
					for (Iterator i=elements.iterator(); i.hasNext(); )
					{
						String nodeName = (String)i.next();
						for (int itemIndex=0; itemIndex < items.length; itemIndex++)
						{
							if (nodeName.equals(items[itemIndex].getText()))
							{
								currentNode = items[itemIndex];
								items = currentNode.getItems();
								t.showItem(currentNode);
								t.setSelection(new TreeItem[] { currentNode });
								currentNode.setExpanded(true);
								
								return;
							}	
						}
						return;
					}
					return;
				}
			}
			setErrorMessage("The depot '"+elements.get(0)+"' is unknown.");
		}	
		catch (Exception e)
		{
			setErrorMessage("Depot Path must be a well-formed Perforce path");
		}	
	}

	public void createControl(Composite parent)
	{
		Composite composite = new Composite(parent, SWT.NULL);
		
		GridLayout layout = new GridLayout();
		GridData data = new GridData(GridData.FILL_BOTH);
		composite.setLayout(layout);
		composite.setLayoutData(data);

		createLabel(composite,"Depot Path:");
		final Text pathControl = createTextField(composite);
		pathControl.addModifyListener(new ModifyListener()
			{
				public void modifyText(ModifyEvent e)
				{
					String path = pathControl.getText();
					showDepotPath(path);
				}
			}
			);
		tree = createDepotTree(composite);	
		tree.addSelectionChangedListener(new ISelectionChangedListener()
			{
				public void selectionChanged(SelectionChangedEvent event)
				{
					IStructuredSelection selection = (IStructuredSelection)event.getSelection();
					RemoteFolder folder = (RemoteFolder)selection.getFirstElement();
					pathControl.setText(folder.getFullName());
				}			
			}
		);

		setControl(composite);		
	}

}
